
*====================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                Processing Vignette modules - Follow Up
*====================================================================*

* CASES IN VIGNETTE DATA FILE 
/*(Case I (VAR ah01-ap04): Pneumonia 
   A young father brings his two year old male child to you
   Father: �Doctor, my child has been suffering from diarrhea for the 
   last two days. Please give him some treatment.�

   Case II (VAR bh01-br07): Diarrhea 
   A young father brings his two year old male child to you
   Father: �Doctor, my child has been suffering from fever and cough for 
   the last    five days. He seems like he is also has some breathing 
   trouble (baccha sans nahi le paa raha hai) Please give him some treatment.)*/


*====================================================================*
* DIARRHEA 
*====================================================================*

* Open data
  use uid-q11b bh01-bp04 using "$rawdatap2\deidentified_vignettes_281114", clear

* Check number of clusters (80) and observations (396)
  duplicates report q04
  assert r(unique_value)==80
  assert r(N)==448

***********************************************
* General Questions
***********************************************

* Source
  gen     source = "Vignette Second Round"
  lab var source "Data source"

* Wave
  gen     wave = 2
  lab var wave "Wave"

* Identify disease
  gen     case = 1
  lab var case "Type of disease"
  lab def case 1 "Diarrhea" 2 "Pneumonia", replace
  lab val case case

* Sample 
/*q11 status of interview: 1: completed; 2: partially completed; 4: facility was closed*/
  gen     sample4 = 1 if q11==1
  replace sample4 = 2 if q11==2
  replace sample4 = 3 if q11==4
  lab var sample4 "Sample"
  lab def sample4 1 "completed" 2 "partially completed" 3 "closed facility"
  lab val sample4 sample4
  tab sample4

* Providers ID
  gen     prov_id = uid
  replace prov_id = 353037 if prov_id==353937
  lab var prov_id "Provider ID"

* Location variables
  gen     district = q02 
  lab var district "District"
  gen     block = q03 
  lab var block "Block"
  gen     cluster = q04
  lab var cluster "Cluster"
  gen     village = q05
  lab var village "Village"

* WHP Providers - NOT OVERSAMPLED
  gen UID = prov_id
  merge 1:1 UID using "$rawdatap2\DeidentifiedProviderListingData_270315", keepus(Provider_Type)
  drop if _merge==2

  gen     TPC = 0 if Provider_Type!=.
  replace TPC = 1 if Provider_Type==3
  lab var TPC "WHP-TPC provider"
  tab TPC, nol
  drop _merge UID Provider_Type

* WHP-RHP Providers - NON-OVERSAMPLED
  merge 1:1 uid using "$rawdatap2\specific_whp_providers_sample", keepus(sky_care_id_1) force

  gen     RHP = 0 if TPC!=. & _merge!=3
  replace RHP = 1 if _merge==3
  lab var RHP "WHP non-oversampled"
  drop _merge sky_care_id_1

* Fixing TPC identification and identifying oversampled
  list prov_id TPC RHP
  list prov_id TPC RHP if TPC==1 | RHP==1
  replace TPC = 0 if TPC==. & RHP==1
  replace TPC = 0 if TPC==1 & RHP==1

  merge 1:1 uid using "$rawdatap2\tpc_providers"
  drop if _merge==2
  replace TPC = 1 if _merge==3

  gen     WHP_oversampled = 0 
  replace WHP_oversampled = 1 if _merge==3
  lab var WHP_oversampled "=1 if oversampled WHP"
  drop _merge cluster_code- village_code

* WHP Providers - ALL
  gen     WHP = 0 if TPC!=.
  replace WHP = 1 if TPC==1 | RHP==1
  lab var WHP "World Health Partners (WHP) provider"

* Interview related variables
  gen     int_code1 = q09a
  lab var int_code1 "Interviewer code"
  gen     int_code2 = q09b
  lab var int_code2 "Interviewer code"

* Survey date
  gen     survey_date=mdy(q08_mm, q08_dd, q08_yyyy)
  format  survey_date %d
  lab var survey_date "Date of survey"

* Label yes/no
  label define yesno 0 "no" 1 "yes"

***********************************************
* Medical History Section - (h)
***********************************************

* Correcting labels
  lab var bh08 "Worms in stools?"
  lab var bh09 "Foul smelling?"
  lab var bh10 "Does he complain of stomachache?"
  lab var bh11 "Does he have tears when he cries?"
  lab var bh12 "Is the child weak now?"
  lab var bh13 "Is the child fussy?"
  lab var bh14 "Vomiting?"
  lab var bh15 "What are his feeding habits"
  lab var bh16 "Is the child eating normally? "
  lab var bh17 "Is the child drinking a lot of water? How many glasses of water does he drink?  "
  lab var bh18 "How many times do you give him milk from the feeding bottle"
  lab var bh19 "Do you boil the feeding bottle"
  lab var bh20 "Where do you get your drinking water from?"
  lab var bh21 "Do you boil your drinking water?"
  lab var bh22 "Do you have a toilet in your house or do you go to the fields?"
  lab var bh23 "Is your house a pucca or kachha house"
  lab var bh24 "Does the child eat mud? Does the child play in the mud?"
  lab var bh25 "Do you wash hands after going to toilet or before feeding the child"
  lab var bh26 "Is the child teething"
  lab var bh27 "Are you giving him any medicines"
  lab var bh28 "Have you given him ORS (jeevan ghol)"

* Recoding variables
  foreach v of varlist bh* {
    recode `v' (2=0)
      }

* Number of Medical History questions asked
  egen    nhistory = rowtotal(bh01-bh29)
  lab var nhistory "No. of Medical History ques. asked"

  gen     phistory = nhistory/29
  lab var phistory "Pct. of Medical History ques. asked"

* CAUSE of disease: Essential questions
  local essen "bh01 bh04 bh07 bh10 bh14 bh15 bh21"
  local nword: word count `essen'

  egen    ncause = rsum(`essen'), missing
  lab var ncause "No. of ques. asked related to cause"

  gen     pcause = ncause/`nword'
  lab var pcause "Pct. of ques. asked related to cause"

* SEVERITY of disease: Essential questions
  local seve "bh02 bh05 bh06 bh12 bh17"
  local nword: word count `seve'

  egen    nseverity = rsum(`seve'), missing
  lab var nseverity "No. of ques. asked related to severity"

  gen     pseverity = nseverity/`nword'
  lab var pseverity "Pct. of ques. asked related to severity"

* Essential items obtained by providers in history-taking
  egen    nessential = rsum(ncause nseverity), missing
  lab var nessential "No. of essential items obtained in history-taking"


***********************************************
* EXAMINATION SECTION - (be)
***********************************************
* Recoding variables
  foreach v of varlist be* {
    recode `v' (2=0)
      }

* Relevant Examination questions
  local exam "be02 be06 be07 be08 be09"
  local nword: word count `exam'

  egen    nexam = rsum(`exam'), missing
  lab var nexam "No. of relevant examination taken"

***********************************************
* Investigation Section - (bi)
***********************************************

* Recoding variables
  foreach v of varlist bi01 bi02 bi03 bi04 {
    recode `v' (2=0)
    }

* No. of investigation questions asked
  egen    ninvest=rsum(bi01 bi02 bi03 bi04), missing
  lab var ninvest "No. of investigation questions asked"

* Other question/examinations done
  tab bi05 bi06a1,m
  gen     other =(bi05==1) if bi05!=.
  replace other = 0 if bi05==1 & bi06a1==.
  lab var other "Other question/examination done"

***********************************************
* Diagnosis - (c#d)
***********************************************

* Gave any diagnosis
  egen aux=rownonmiss(bd01a1 bd01a2), strok
  gen     dx_any = 0
  replace dx_any = 1 if aux==1
  lab var dx_any "=1 if any kind of diagnosis was given"
  drop aux

* Type of Diagnosis
  gen     dx = bd01a1 + bd01a2
  gen sound=soundex(dx)
  replace dx = "DIARRHEA"     if (sound=="D600" | sound=="D640" | sound=="D660") & dx!="DAIRIYA HUA HAI"
  replace dx = "LOOSE MOTION" if (sound=="L253" | sound=="L500")
  replace dx = "COLD DIARRHEA" if (sound=="G436" | | sound=="C436")
  replace dx = "DYSENTERY"    if sound=="D253" | dx=="PAKHHANA LOOSE MOTION"
  replace dx=subinstr(dx, "DIARRHOEA","DIARRHEA",.)
  lab var dx "Diagnosis"
  drop sound

* Correct diagnosis
  gen     dx_type = 0
  replace dx_type = 1 if dx=="DIARRHEA"
  replace dx_type = 1 if dx=="LOOSE MOTION"
  lab var dx_type "=1 if diagnosis was correct"
  lab def type 1 "correct" 0 "incorrect"
  lab val dx_type type

***********************************************
* Treatment
***********************************************

* Gave Any Medication/Treatment
  forvalues n=1/2 {
    foreach l in a b c d e f g {
      replace bt0`n'`l'5=. if bt0`n'`l'5!=. & bt0`n'`l'1==""
      }
    }

  egen    tx_any=rownonmiss(bt01?5 bt02?5) 
  recode  tx_any (2/max=1)
  replace tx_any = 1 if bt04==1 | bt07==1
  lab var tx_any "=1 if any treatment was given"
  lab val tx_any yesno

***********************************************
* Correct Treatment
***********************************************

* Merge
  sort prov_id
  merge 1:1 prov_id case using "$rawdatap2\correct_treatment_vig.dta"
  keep if _merge==3
  drop _merge
  drop tx_detail3

* Rename
  rename tx_detail1 tx_detail
  lab var tx_detail "detailed treatment"
  lab var nmedicines "# of medicines prescribed"
  lab var nantibiotics "# of antibiotics"

***********************************************
* Recomendations
***********************************************

* Referral
  gen     rec_referral = (bt13==1)
  lab var rec_referral "Recommendation for referral"
  lab val rec_referral yesno

* Changes in diet,hygiene,changes in living conditions
  gen     rec_hygiene_diet = (bt11==1|bt12==1)
  lab var rec_hygiene_diet "Recs. for changes in diet, hygiene and living conditions"
  lab val rec_hygiene_diet yesno

* Return
  gen     rec_return = (bt10==1)
  lab var rec_return "Recs. on when to return or how to recognize a worsening situation"

* ORS
  egen    rec_ors = anymatch(bt04 bt07), v(1)
  replace rec_ors = 2 if bt05==1 | bt08==1
  replace rec_ors = 3 if bt06==1 | bt09==1
  lab var rec_ors "Recs. related to ORS - ONLY homemade solution" 
  lab def rec_ors 0 "ORS not recommended" 1 "ORS recommended" 2 "ORS recommended + preparation" 3 "ORS recommended + boiling water"
  lab val rec_ors rec_ors

***********************************************
* Particular Treatment
***********************************************
* Different Treatment because poor
  gen     poor_tx = bpt07
  lab var poor_tx "What would you do if the patient were poor?"
  lab val poor_tx bpt07

* Medicines because of poor
  gen     med_ifpoor1=bpt08a5
  lab var med_ifpoor1 "Medicine 1 if poor"
  lab val med_ifpoor1 medcode
  gen     med_ifpoor2=bpt08b5
  lab var med_ifpoor2 "Medicine 2 if poor"
  lab val med_ifpoor2 medcode
  gen     med_ifpoor3=bpt08c5
  lab var med_ifpoor3 "Medicine 3 if poor"
  lab val med_ifpoor3 medcode
  gen     med_ifpoor4=bpt08d5
  lab var med_ifpoor4 "Medicine 4 if poor"
  lab val med_ifpoor4 medcode


* Test recommended if poor
  gen     poor_test = bpt05
  lab var poor_test "if this patient was very poor,what tests would you recommend"
  lab val poor_test bpt07

* Test if poor
  gen     test_ifpoor11=bpt06a2
  lab var test_ifpoor11 "Test if poor"

* Fee - different scenarios
  gen     fee1 = bp01
  lab var fee1 " first time fee (including medicines)"

  gen     fee2 = bp02 
  lab var fee2 "consultation + doubled amount of medicines"

  gen     fee3 = bp03 
  lab var fee3 "consultation + no medicines"

  gen     fee4 = bp04
  lab var fee4 "second time fee (including medicines)"

***********************************************
* ITR
***********************************************

/*Selected items
  1  bh01 (c1h1) does the child have fever is the child warm
  2  bh02 (c1h2) is the urine normal /urine color
  3  bh03 (c1h3) when did the child last urinate
  4  bh04 (c1h4) nature of stool
  5  bh05 (c1h5) frequency of stool
  6  bh06 (c1h6) quantity of stool
  7  bh07 (c1h7) blood / mucus in the stools
  8  bh08 (c1h8) worms in stools
  9  bh09 (c1h9) foul smelling
  10 bh10 (c1h10) does he complain of stomachache
  11 bh11 Does he have tears when he cries? - (c1h11) does he cry while passing stools
  12 bh12 (c1h13) is the child weak now
  13 bh14 (c1h14) vomiting
  14 bh17 (c1h19) Is the child drinking a lot of water? How many glasses of water does he drink?
  15 bh26 (c1h30) is the child teething
  16 be02 (c1e2) weight of child
  17 be06 (c1e6) temperature
  18 be07 (c1e7) mucous membranes for moistness
  19 be08 (c1e8) skin color and turgor
  20 be09 (c1e9) palpation of the abdomen */

* Selected items should begin with the same string
  local i 1
  foreach v of varlist bh01-bh12 bh14 bh17 bh26 be02 be06-be09 {
  local varlab`i' : variable label `v'
  gen vig_item`i'=`v'
  lab var vig_item`i' "`varlab`i''"
  local i= `i' +1
    }

* Estimate theta for providers who treated cases of diarrhea
  preserve
  keep if sample4==1
  keep prov_id vig_item*
  openirt, id(prov_id) item_prefix(vig_item) save_item_parameters("$irtdata2\vig_items_parameters_1.dta") save_trait_parameters("$irtdata2\vig_traits_1.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata2\vig_traits_1.dta"
  renpfix theta vig_theta
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_std = std(vig_theta_eap)
  lab var vig_theta_eap_std "Standardized Theta (EAP)"
  egen    vig_theta_mle_std = std(vig_theta_mle)
  lab var vig_theta_mle_std "Standardized Theta (MLE)"
  drop vig_theta_pv1-vig_theta_pv5 vig_theta_mle_se


***********************************************
* Know-do Gap
* 12 Main questions
***********************************************

* Diarrhea
  egen vig_dq1 = anymatch(bh04 bh07 bh08 bh09), v(1)
  gen  vig_dq2 = bh05
  gen  vig_dq3 = bh06
  egen vig_dq4 = anymatch(bh02 bh03), v(1)
  gen  vig_dq5 = bh12
  gen  vig_dq6 = bh01
  gen  vig_dq7 = bh10
  gen  vig_dq8 = bh14
  egen vig_dq9 = anymatch(bh20 bh21), v(1)
  egen vig_dq10 = anymatch(bh15 bh16), v(1)
  gen  vig_dq11 = bh17
  egen vig_dq12 = anymatch(bh22 bh23 bh24), v(1)

  lab var vig_dq1 "Diarrhea: Qualities of stool"
  lab var vig_dq2 "Diarrhea: Frequency"
  lab var vig_dq3 "Diarrhea: Quantity of stool"
  lab var vig_dq4 "Diarrhea: Questions about urination"
  lab var vig_dq5 "Diarrhea: Is the child active/playful?"
  lab var vig_dq6 "Diarrhea: Fever"
  lab var vig_dq7 "Diarrhea: Abdominal pain"
  lab var vig_dq8 "Diarrhea: Vomiting"
  lab var vig_dq9 "Diarrhea: Source of drinking water"
  lab var vig_dq10 "Diarrhea: What has the child eaten?"
  lab var vig_dq11 "Diarrhea: Taking fluids"
  lab var vig_dq12 "Diarrhea: Physical Environment"

* IRT Performance based on 12 questions

* Estimate theta for providers who treated cases of diarrhea
  preserve
  keep if sample4==1
  keep prov_id vig_dq1-vig_dq12
  openirt, id(prov_id) item_prefix(vig_dq) save_item_parameters("$irtdata2\vig_items_parameters_1_q12.dta") save_trait_parameters("$irtdata2\vig_traits_1_q12.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata2\vig_traits_1_q12.dta", keepus(theta_eap theta_mle) 
  rename theta_eap vig_theta_eap_q12
  rename theta_mle vig_theta_mle_q12
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_q12_std = std(vig_theta_eap_q12)
  lab var vig_theta_eap_q12_std "Standardized Theta (EAP)"
  egen    vig_theta_mle_q12_std = std(vig_theta_mle_q12)
  lab var vig_theta_mle_q12_std "Standardized Theta (MLE)"

* Estimate theta for providers who treated cases of diarrhea and are not oversampled WHP
  preserve
  keep if sample4==1 & WHP_oversampled==0
  keep prov_id vig_dq1-vig_dq12
  openirt, id(prov_id) item_prefix(vig_dq) save_item_parameters("$irtdata2\vig_items_parameters_1_q12b.dta") save_trait_parameters("$irtdata2\vig_traits_1_q12b.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata2\vig_traits_1_q12b.dta", keepus(theta_eap theta_mle) 
  rename theta_eap vig_theta_eap_q12b
  rename theta_mle vig_theta_mle_q12b
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_q12_stdb = std(vig_theta_eap_q12b)
  lab var vig_theta_eap_q12_stdb "Standardized Theta (EAP)"
  egen    vig_theta_mle_q12_stdb = std(vig_theta_mle_q12b)
  lab var vig_theta_mle_q12_stdb "Standardized Theta (MLE)"


***********************************************
* Correcting District code to match with Baseline
***********************************************
  rename district district_or
  recode district_or (2=3) (3=5) (4=7) (5=4) (6=10) (7=8) (8=6) (9=11) (10=2) (11=9), gen(district)
  order district, after(district_or)


***********************************************
* Changes in cluster 387
***********************************************

  replace block = 134 if block==142 & cluster==387
  replace cluster = 462 if cluster ==387

***********************************************
* Saving processed data
***********************************************
  gen last=.
  keep source-last
  drop last
  sort prov_id
  label data "Vignette Diarrhea Second Round"
  compress
  save "$prodata2\vignette_diarrhea", replace




*====================================================================*
* PNEUMONIA
*====================================================================*

* Open data
  use uid-ap04 using "$rawdatap2\deidentified_vignettes_281114", clear

* Check number of clusters (80) and observations (396)
  duplicates report q04
  assert r(unique_value)==80
  assert r(N)==448

***********************************************
* General Questions
***********************************************

* Source
  gen     source = "Vignette Second Round"
  lab var source "Data source"

* Wave
  gen     wave = 2
  lab var wave "Wave"

* Identify disease
  gen     case = 2
  lab var case "Type of disease"
  lab def case 1 "Diarrhea" 2 "Pneumonia", replace
  lab val case case

* Sample 
/*q11 status of interview: 1: completed; 2: partially completed; 4: facility was closed*/
  gen     sample4 = 1 if q11==1
  replace sample4 = 2 if q11==2
  replace sample4 = 3 if q11==4
  lab var sample4 "Sample"
  lab def sample4 1 "completed" 2 "partially completed" 3 "closed facility"
  lab val sample4 sample4
  tab sample4

* Providers ID
  gen     prov_id = uid
  replace prov_id = 353037 if prov_id==353937
  lab var prov_id "Provider ID"

* Location variables
  gen     district = q02 
  lab var district "District"
  gen     block = q03 
  lab var block "Block"
  gen     cluster = q04
  lab var cluster "Cluster"
  gen     village = q05
  lab var village "Village"

* WHP Providers - NOT OVERSAMPLED
  gen UID = prov_id
  merge 1:1 UID using "$rawdatap2\DeidentifiedProviderListingData_270315", keepus(Provider_Type)
  drop if _merge==2

  gen     TPC = 0 if Provider_Type!=.
  replace TPC = 1 if Provider_Type==3
  lab var TPC "WHP-TPC provider"
  tab TPC, nol
  drop _merge UID Provider_Type

* WHP-RHP Providers - NON-OVERSAMPLED
  merge 1:1 uid using "$rawdatap2\specific_whp_providers_sample", keepus(sky_care_id_1) force

  gen     RHP = 0 if TPC!=. & _merge!=3
  replace RHP = 1 if _merge==3
  lab var RHP "WHP non-oversampled"
  drop _merge sky_care_id_1

* Fixing TPC identification and identifying oversampled
  list prov_id TPC RHP
  list prov_id TPC RHP if TPC==1 | RHP==1
  replace TPC = 0 if TPC==. & RHP==1
  replace TPC = 0 if TPC==1 & RHP==1

  merge 1:1 uid using "$rawdatap2\tpc_providers"
  drop if _merge==2
  replace TPC = 1 if _merge==3

  gen     WHP_oversampled = 0 
  replace WHP_oversampled = 1 if _merge==3
  lab var WHP_oversampled "=1 if oversampled WHP"
  drop _merge cluster_code- village_code


* WHP Providers - ALL
  gen     WHP = 0 if TPC!=.
  replace WHP = 1 if TPC==1 | RHP==1
  lab var WHP "World Health Partners (WHP) provider"

* Interview related variables
  gen     int_code1 = q09a
  lab var int_code1 "Interviewer code"
  gen     int_code2 = q09b
  lab var int_code2 "Interviewer code"

* Survey date
  gen     survey_date=mdy(q08_mm, q08_dd, q08_yyyy)
  format  survey_date %d
  lab var survey_date "Date of survey"

* Label yes/no
  label define yesno 0 "no" 1 "yes"

***********************************************
* Medical History Section - (h)
***********************************************

* Recoding variables
  foreach v of varlist ah* {
    recode `v' (2=0)
      }

* Number of Medical History questions asked
  egen    nhistory = rowtotal(ah01-ah28)
  lab var nhistory "No. of Medical History ques. asked"

  gen     phistory = nhistory/28
  lab var phistory "Pct. of Medical History ques. asked"

* CAUSE of disease: Essential questions
  local essen "ah01 ah09 ah11 ah12 ah13"
  local nword: word count `essen'

  egen    ncause = rsum(`essen'), missing
  lab var ncause "No. of ques. asked related to cause"

  gen     pcause = ncause/`nword'
  lab var pcause "Pct. of ques. asked related to cause"

* SEVERITY of disease: Essential questions
  local seve "ah16 ah06"
  local nword: word count `seve'

  egen    nseverity = rsum(`seve'), missing
  lab var nseverity "No. of ques. asked related to severity"

  gen     pseverity = nseverity/`nword'
  lab var pseverity "Pct. of ques. asked related to severity"

* Essential items obtained by providers in history-taking
  egen    nessential = rsum(ncause nseverity), missing
  lab var nessential "No. of essential items obtained in history-taking"


***********************************************
* EXAMINATION SECTION - (be)
***********************************************
* Recoding variables
  foreach v of varlist ae* {
    recode `v' (2=0)
      }

* Relevant Examination questions
  local exam "ae02 ae03 ae04 ae05 ae06 ae09"
  local nword: word count `exam'

  egen    nexam = rsum(`exam'), missing
  lab var nexam "No. of relevant examination taken"

***********************************************
* Investigation Section - (bi)
***********************************************
* Recoding variables
  foreach v of varlist ai01-ai05 {
    recode `v' (2=0)
    }

* Recoding values according to extra questions
  /*(next time, not possible now given limited time)*/

* No. of investigation questions asked
  egen    ninvest=rsum(ai01-ai05), missing
  lab var ninvest "No. of investigation questions asked"

* Other question/examinations done
  tab ai05 ai06a1,m
  gen     other =(ai05==1) if ai05!=.
  replace other = 0 if ai05==1 & ai06a1==.
  lab var other "Other question/examination done"

***********************************************
* Diagnosis - (c#d)
***********************************************

* Gave any diagnosis
  egen aux=rownonmiss(ad01 ad02), strok
  gen     dx_any = 0
  replace dx_any = 1 if aux==1 & ad02!="CAN'T SAY"
  lab var dx_any "=1 if any kind of diagnosis was given"
  drop aux

* Type of Diagnosis
/*(NEED TO CLEAN MORE THE DIAGNOSIS VARIBALES - translate hindi)*/
  gen     dx = ad01 + ad02
  lab var dx "Diagnosis"

* Correct diagnosis
  gen     dx_type = 0
  replace dx_type = 1 if dx=="PNEUMONIA"
  lab var dx_type "=1 if diagnosis was correct"
  lab def type 1 "correct" 0 "incorrect"
  lab val dx_type type

***********************************************
* Treatment
***********************************************

* Gave Any Medication/Treatment

  forvalues n=1/2 {
    foreach l in a b c d e f g {
      replace at0`n'`l'05=. if at0`n'`l'05!=. & at0`n'`l'01==""
      }
    }

  egen    tx_any=rownonmiss(at01?05 at02?05) 
  recode  tx_any (2/max=1)
  lab var tx_any "=1 if any treatment was given"
  lab val tx_any yesno

***********************************************
* Correct Treatment
***********************************************

* Merge
  sort prov_id
  merge 1:1 prov_id case using "$rawdatap2\correct_treatment_vig.dta"
  keep if _merge==3
  drop _merge
  drop tx_detail1

* Rename
  rename tx_detail3 tx_detail
  lab var tx_detail "detailed treatment"
  lab var nmedicines "# of medicines prescribed"
  lab var nantibiotics "# of antibiotics"

***********************************************
* Recomendations
***********************************************

* Referral
  gen     rec_referral = (at03==1)
  lab var rec_referral "Recommendation for referral"
  lab val rec_referral yesno

* Changes in diet,hygiene,changes in living conditions
  gen     rec_hygiene_diet = (at06==1|at07==1)
  lab var rec_hygiene_diet "Recs. for changes in diet, hygiene and living conditions"
  lab val rec_hygiene_diet yesno

* Hospitalization
  gen     hospitalization=(at04==1)
  lab var hospitalization "Hospitalizacion recommended for Pneumonia"

* Return
  gen     rec_return = (at05==1)
  lab var rec_return "Recs. on when to return or how to recognize a worsening situation"

***********************************************
* Particular Treatment
***********************************************
* Different Treatment because poor
  gen     poor_tx = apt05
  lab var poor_tx "What would you do if the patient were poor?"
  lab val poor_tx apt05

* Medicines because of poor
  gen     med_ifpoor1=apt06a6
  lab var med_ifpoor1 "Medicine 1 if poor"
  lab val med_ifpoor1 medcode
  gen     med_ifpoor2=apt06b6
  lab var med_ifpoor2 "Medicine 2 if poor"
  lab val med_ifpoor2 medcode
  gen     med_ifpoor3=apt06c6
  lab var med_ifpoor3 "Medicine 3 if poor"
  lab val med_ifpoor3 medcode
  gen     med_ifpoor4=apt06d6
  lab var med_ifpoor4 "Medicine 4 if poor"
  lab val med_ifpoor4 medcode

* Test recommended if poor
  gen     poor_test = apt03
  lab var poor_test "if this patient was very poor,what tests would you recommend"
  lab val poor_test apt03

* Test if poor
  gen     test_ifpoor11=apt04a2
  lab var test_ifpoor11 "Test if poor"

* Fee - different scenarios
  gen     fee1 = ap01
  lab var fee1 " first time fee (including medicines)"

  gen     fee2 = ap02 
  lab var fee2 "consultation + doubled amount of medicines"

  gen     fee3 = ap03 
  lab var fee3 "consultation + no medicines"

  gen     fee4 = ap04
  lab var fee4 "second time fee (including medicines)"


***********************************************
* ITR
***********************************************

* Selected items should begin with the same string
  local i 1
  foreach v of varlist ah01 ah02 ah03 ah04 ah09 ah11 ah12 ah15 ah19 ae02-ae06 ae09 ai01 ai04 {
  local varlab`i' : variable label `v'
  gen vig_item`i'=`v'
  lab var vig_item`i' "`varlab`i''"
  local i= `i' +1
    }

  egen    vig_item18=anymatch(ai03a-ai03e), v(1)
  lab var vig_item18 "WBC differential - neutrophils,basophils,eosinophils,lymphoyctes,monocytes"

* Estimate theta for providers who treated cases of pneumonia
  preserve
  keep if sample4==1
  keep prov_id vig_item*
  openirt, id(prov_id) item_prefix(vig_item) save_item_parameters("$irtdata2\vig_items_parameters_2.dta") save_trait_parameters("$irtdata2\vig_traits_2.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata2\vig_traits_2.dta"
  renpfix theta vig_theta
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_std = std(vig_theta_eap)
  lab var vig_theta_eap_std "Standardized Theta (EAP)"
  egen    vig_theta_mle_std = std(vig_theta_mle)
  lab var vig_theta_mle_std "Standardized Theta (MLE)"
  drop vig_theta_pv1-vig_theta_pv5 vig_theta_mle_se


***********************************************
* Know-do Gap
* 12 Main questions
***********************************************

* Pneumonia
  egen vig_pq1 = anymatch(ah01 ah03), v(1)
  gen  vig_pq2 = ah09
  egen vig_pq3 = anymatch(ah11 ah12 ah13 ah14), v(1)
  egen vig_pq4 = anymatch(ah02), v(1)
  gen  vig_pq5 = ah04
  gen  vig_pq6 = ah15
  gen  vig_pq7 = ah16
  gen  vig_pq8 = ah06
  egen vig_pq9 = anymatch(ah19 ah22 ah24), v(1)
  egen vig_pq10 = anymatch(ah18 ah20), v(1)
  gen  vig_pq11 = ah27
  gen  vig_pq12 = ah17

  lab var vig_pq1 "Pneumonia: Fever"
  lab var vig_pq2 "Pneumonia: Breathing is rapid"
  lab var vig_pq3 "Pneumonia: Difficulty in breathing/nostrils flaring/skin between ribs moves inwards/neck muscles are strained"
  lab var vig_pq4 "Pneumonia: Type of cough"
  lab var vig_pq5 "Pneumonia: Runny nose or nose blocked"
  lab var vig_pq6 "Pneumonia: Sounds while breathing"
  lab var vig_pq7 "Pneumonia: Change in colour of fingernails, hand, toes"
  lab var vig_pq8 "Pneumonia: Child is weak"
  lab var vig_pq9 "Pneumonia: Breastfeeding/immunization history"
  lab var vig_pq10 "Pneumonia: History of asthma/alleries"
  lab var vig_pq11 "Pneumonia: Have a chula at home"
  lab var vig_pq12 "Pneumonia: Other children have similar fever/cough"

* IRT Performance based on 12 questions

* Estimate theta for providers who treated cases of pneumonia
  preserve
  keep if sample4==1
  keep prov_id vig_pq1-vig_pq12
  openirt, id(prov_id) item_prefix(vig_pq) save_item_parameters("$irtdata2\vig_items_parameters_2_q12.dta") save_trait_parameters("$irtdata2\vig_traits_2_q12.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata2\vig_traits_2_q12.dta", keepus(theta_eap theta_mle) 
  rename theta_eap vig_theta_eap_q12
  rename theta_mle vig_theta_mle_q12
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_q12_std = std(vig_theta_eap_q12)
  lab var vig_theta_eap_q12_std "Standardized Theta (EAP)"
  egen    vig_theta_mle_q12_std = std(vig_theta_mle_q12)
  lab var vig_theta_mle_q12_std "Standardized Theta (MLE)"

* Estimate theta for providers who treated cases of pneumonia and are not oversampled WHP
  preserve
  keep if sample4==1 & WHP_oversampled==0
  keep prov_id vig_pq1-vig_pq12
  openirt, id(prov_id) item_prefix(vig_pq) save_item_parameters("$irtdata2\vig_items_parameters_2_q12b.dta") save_trait_parameters("$irtdata2\vig_traits_2_q12b.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata2\vig_traits_2_q12b.dta", keepus(theta_eap theta_mle) 
  rename theta_eap vig_theta_eap_q12b
  rename theta_mle vig_theta_mle_q12b
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_q12_stdb = std(vig_theta_eap_q12b)
  lab var vig_theta_eap_q12_stdb "Standardized Theta (EAP)"
  egen    vig_theta_mle_q12_stdb = std(vig_theta_mle_q12b)
  lab var vig_theta_mle_q12_stdb "Standardized Theta (MLE)"

***********************************************
* Correcting District code to match with Baseline
***********************************************
  rename district district_or
  recode district_or (2=3) (3=5) (4=7) (5=4) (6=10) (7=8) (8=6) (9=11) (10=2) (11=9), gen(district)
  order district, after(district_or)

***********************************************
* Changes in cluster 387
***********************************************

  replace block = 134 if block==142 & cluster==387
  replace cluster = 462 if cluster ==387

***********************************************
* Saving processed data
***********************************************
  gen last=.
  keep source-last
  drop last
  sort prov_id
  label data "Vignette Pneumonia Second Round"
  compress
  save "$prodata2\vignette_pneumonia", replace
